﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;

namespace HZ.DrugTrade.Web
{
    public partial class getstatus : System.Web.UI.Page
    {


        IBLL_Company IBC = Factory.BLL_Company();

        IBLL_SelectRelation iselectRelation = Factory.BLL_SelectRelation();
        //存放地区编号名称信息的字典
        Dictionary<string, string> dicRule = new Dictionary<string, string>();

        int intOver = 0;
        int intNotAll = 0;
        int intNo=0;
        int intCompanyAll = 0;
        int intCompanyIng = 0;

        int intCountRelation = 0;//可配送的配送关系数量
        int intCountTB = 0;//已提交确认信息的投标企业数量
        int intCountPS = 0;//可配送的配送企业数量

        protected void Page_Load(object sender, EventArgs e)
        {
            setReadme();
        }

        protected void test_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 获取未配送的地区
        /// </summary>
        /// <param name="strIDs"></param>
        /// <returns></returns>
        protected string GetUnDisAreaNames(string strIDs)
        {
            string strAreaNames = string.Empty;
            foreach (string AreaID in dicRule.Keys)
            {
                if (strIDs.Contains(AreaID) == false)
                {
                    strAreaNames += dicRule[AreaID] + "、";
                }
            }
            return strAreaNames;
        }
        /// <summary>
        /// 
        /// </summary>
        protected void setReadme()
        {

            int iccount = IBC.GetAllCompanyCount();

            #region 代码逻辑和check函数一样
            //获取该企业的中标目录

            HZ.DrugTrade.Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            //procureCatalog.CompanyID_TB = CurrentAccount.GetUser().Org.OrgID;
            procureCatalog.SortFields = " companyid_TB ";

            int totalrecord = 0;
            IBLL_ProcurementBasic iDistribution = Factory.BLL_ProcurementBasic();

            DataTable dtAll = iDistribution.GetProcureCatalogDTByWhere2(procureCatalog, 1, 0, out totalrecord);

            int totalCount = 0;
            if (totalrecord > 0)
            {
                dtAll.Columns.Add(new DataColumn("AreaNames", Type.GetType("System.String")));

                //获取企业的配送关系
                string strWhere = " ";
                //配送关系
                strWhere += "  order by companyid_TB,ProcureCatalogID ";

                DataTable dt = iselectRelation.GetSelectRelationToConfirm2(strWhere, 1, 0, out totalCount);

                getcount(dtAll, dt, totalCount, totalrecord);


            }
            #endregion

            #region 
            DataTable dtEnd = iselectRelation.GetAreaDistribution();
            if (dtEnd.Rows.Count == 3)
            {
                foreach (DataRow row in dtEnd.Rows)
                {
                    if (row["t"].ToString() == "relation")
                    {
                        intCountRelation = int.Parse(row["c"].ToString());
                        continue;
                    }
                    if (row["t"].ToString() == "tb")
                    {
                        intCountTB = int.Parse(row["c"].ToString());
                        continue;
                    } if (row["t"].ToString() == "ps")
                    {
                        intCountPS = int.Parse(row["c"].ToString());
                        continue;
                    }
                }
            }
            #endregion
            this.lblReadme.Text = string.Format(@"配送企业数量：{0}；<br/>
            已建立配送关系数量：{1}；<br/>
            入围品规数量：{7}；<br/>
已经覆盖全区的入围品规数量：{2}；<br/>未覆盖全区的入围品规数量：{3}；<br/>
未选择配送的入围品规数量：{4}；<br/>总投标企业数：{5}；<br/>未完成投标企业数：{6}；<br/>完成投标企业数：{8}；<hr/> 状态为可配送的配送关系数量：{9}；<br/>已提交确认信息的投标企业数量：{10}；<br/>可配送的配送企业数量：{11}；<br/>",
 iccount.ToString(), totalCount.ToString(), intOver.ToString(), intNotAll.ToString(), intNo.ToString(), intCompanyAll.ToString(), intCompanyIng.ToString(), totalrecord.ToString(), (intCompanyAll - intCompanyIng).ToString()
 , intCountRelation, intCountTB, intCountPS);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected int getcount(DataTable dtAll, DataTable dt, int totalCount, int totalrecord)
        {
            intCompanyAll = 1;
            for (int _index = 1; _index < dtAll.Rows.Count; _index++)
            {
                if (dtAll.Rows[_index - 1]["companyid_TB"].ToString() != dtAll.Rows[_index]["companyid_TB"].ToString())
                {
                    intCompanyAll++;
                }
            }
            if (totalCount <= 0)
            {
                foreach (DataRow row in dtAll.Rows)
                {
                    row["AreaNames"] = "全区";
                }
            }
            else
            {
                #region 十二盟市信息
                if (dicRule.Count != 12)
                {
                    dicRule.Clear();
                    dicRule.Add("150100", "呼和浩特市");
                    dicRule.Add("150200", "包头市");
                    dicRule.Add("150300", "乌海市");
                    dicRule.Add("150400", "赤峰市");
                    dicRule.Add("150500", "通辽市");
                    dicRule.Add("150600", "鄂尔多斯市");
                    dicRule.Add("150700", "呼伦贝尔市");
                    dicRule.Add("150800", "巴彦淖尔市");
                    dicRule.Add("150900", "乌兰察布市");
                    dicRule.Add("152200", "兴安盟");
                    dicRule.Add("152500", "锡林郭勒盟");
                    dicRule.Add("152900", "阿拉善盟");
                }
                #endregion

                //配送关系检查
                for (int _index = 0; _index < totalrecord; _index++)
                {
                    string strProcureCatalogID = dtAll.Rows[_index]["ProcureCatalogID"].ToString();

                    DataRow[] dataRows = dt.Select("ProcureCatalogID='" + strProcureCatalogID + "'", "AreaID");

                    if (dataRows.Length == 0)
                    {
                        dtAll.Rows[_index]["AreaNames"] = "全区";
                    }
                    else
                    {
                        string strAreaNames = string.Empty;

                        foreach (DataRow dr in dataRows)
                        {
                            strAreaNames += dr["AreaID"].ToString() + ",";
                        }

                        dtAll.Rows[_index]["AreaNames"] = GetUnDisAreaNames(strAreaNames);
                    }
                }

                //配送关系检查
                for (int _index = 0; _index < dtAll.Rows.Count; )
                {

                    string strAreaNames = dtAll.Rows[_index]["AreaNames"].ToString();
                    if (strAreaNames.Length <= 0)
                    {
                        dtAll.Rows.RemoveAt(_index);
                        intOver++;
                    }
                    else
                    {
                        if (strAreaNames.Equals("全区"))
                        {
                            intNo++;
                        }
                        else
                        {
                            intNotAll++;
                        }
                        _index++;
                    }
                }
            }

            intCompanyIng = 1;
            for (int _index = 1; _index < dtAll.Rows.Count; _index++)
            {
                if (dtAll.Rows[_index - 1]["companyid_TB"].ToString() != dtAll.Rows[_index]["companyid_TB"].ToString())
                {
                    intCompanyIng++;
                }
            }

            return 0;
        }
    }
}
